﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Workflow;
using Xrm.Framework.Test.Unit.Fakes;
using Microsoft.QualityTools.Testing.Fakes;
using Xrm.Solutions.FrameworkSample.Workflow;

namespace Xrm.Solutions.FrameworkSample.XrmUnitTests.WFActivities
{
    [TestClass]
    public class TestIdentityCheck : WFActivityUnitTest
    {
        #region Instance Variables
        //TODO: Declare your variables for setup and verification
        #endregion
        #region Test Context
        private TestContext context;
        public TestContext TestContext
        {
            get { return context; }
            set { context = value; }
        }
        #endregion
        #region Setup
        protected override Activity SetupActivity()
        {
            //TODO: Setup your Stubs & Shim
            string firstname = context.DataRow[0].ToString();
            string lastname = context.DataRow[1].ToString();
            DateTime dob = (DateTime)context.DataRow[2];
            string address = context.DataRow[3].ToString();
            int score = Int32.Parse(context.DataRow[4].ToString());
            Xrm.Solutions.FrameworkSample.Common.Fakes.ShimIdentityCheckService.AllInstances.RunCheckStringStringDateTimeString =
            (service, fn, ln, d, a) =>
            {
                return score;
            };
            //TODO: Create your Activity
            Activity activity = new IdentityCheck()
            {
                FirstName = firstname,
                LastName = lastname,
                DateOfBirth = dob,
                Address = address
            };
            return activity;
        }
        #endregion
        #region Test
        
        [TestMethod]
        [DeploymentItem("IdentityCheckData.xlsx")]
        [DataSource("System.Data.Odbc",
        "Dsn=Excel Files;Driver={Microsoft Excel Driver(*.xlsx)};dbq=|DataDirectory|\\IdentityCheckData.xlsx;defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true",
        "Customers$",
        DataAccessMethod.Sequential)]
        public void RunTestIdentityCheck()
        {
            using (ShimsContext.Create())
            {
                base.Test();
            }
        }
        #endregion
        #region Verify
        protected override void Verify()
        {
            //TODO: Add Additional Verification
            string decision = context.DataRow[5].ToString();
            string exception = context.DataRow[6].ToString();
            if (!string.IsNullOrEmpty(exception))
            {
                Assert.IsNotNull(base.Error);
                Assert.IsInstanceOfType(base.Error, typeof(InvalidPluginExecutionException));
                Assert.AreEqual(exception, base.Error.Message);
            }
            if (!string.IsNullOrEmpty(decision))
            {
                Assert.IsNull(base.Error);
                string output = base.Outputs["Result"].ToString();
                Assert.AreEqual(decision, output);
            }
        }
        #endregion
    }
}
